package br.unifor.cct.mia.initialize;

import java.util.ArrayList;

import br.unifor.cct.mia.evolutionary.CobwebGenotype;
import br.unifor.cct.mia.evolutionary.Genotype;
import br.unifor.cct.mia.ga.Ga;
import br.unifor.cct.mia.util.Methods;

public class ExclusiveInitializeCU implements Initialize {

	public Object execute(Object value) {	
		Object[] o = (Object[]) value;		
		int size = ((Integer)(o)[0]).intValue();
		Ga Ga = (Ga)o[1];
		
		Ga.population = new CobwebGenotype[Ga.configurations.getPopsize() + 1];
        
        for (int i = 0; i < Ga.configurations.getPopsize(); i++) {
        	
        	Ga.population[i] = new CobwebGenotype(Ga.configurations);        	
        	Ga.population[i].setFitness(0);
        	Ga.population[i].setSelectProbability(0);                 
            
        	ArrayList list = generateValues(0,size);
            for (int j = 0; j < Ga.configurations.getNvars()-2; j++) {
            	((Genotype)Ga.population[i]).setLower(0, j);
            	((Genotype)Ga.population[i]).setUpper(size-1, j); 	       
            	((Genotype)Ga.population[i]).setInteger(true, j);
            	int val = Methods.randIntValue(0,list.size()-1);
            	int valor = ((Integer)list.remove(val)).intValue();            	
            	((Genotype)Ga.population[i]).setGene(valor,j);
            }     
            
            int genSize = Ga.configurations.getNvars();
            
            ((Genotype)Ga.population[i]).setLower(0, genSize-1);
            ((Genotype)Ga.population[i]).setUpper(1, genSize-1); 	       
            ((Genotype)Ga.population[i]).setInteger(false, genSize-1);        	            	
        	((Genotype)Ga.population[i]).setGene(Methods.randDoubleValue(0,1),genSize-1);
        	
        	((Genotype)Ga.population[i]).setLower(0, genSize-2);
        	((Genotype)Ga.population[i]).setUpper(1, genSize-2); 	       
        	((Genotype)Ga.population[i]).setInteger(false, genSize-2);        	            	
        	((Genotype)Ga.population[i]).setGene(Methods.randDoubleValue(0,1),genSize-2);
        }
	        
		return Ga.population;
	}
	
	private ArrayList generateValues(int low, int up) {
		ArrayList result = new ArrayList();
		for ( int i=low; i<up; i++ ) {
			result.add(new Integer(i));
		}
		return result;
	}

}
